﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.css {
    import flash.events.*;
    import reprise.external.*;
    import reprise.events.*;
    import reprise.utils.*;
    import flash.utils.*;
    import reprise.data.collection.*;
    import reprise.css.propertyparsers.*;

    public class CSS extends AbstractResource {

        protected var m_importsTotal:int;
        protected var m_baseURL:String;
        protected var m_importQueue:Array;
        protected var m_loader:ResourceLoader;
        protected var m_runtimeParserRegistered:Boolean;
        protected var m_cleanupTime:int;
        protected var m_importsLoaded:int;
        protected var m_cssVariables:Object;
        protected var m_cssFile:CSSImport;
        protected var m_parseTime:int;
        protected var m_imagePreloadingResource:ResourceLoader;
        private var m_stylesheetURLs:Array;
        protected var m_declarationList:CSSDeclarationList;
        protected var g_idSource:int;
        protected var m_cssSegments:IndexedArray;

        public static const PROPERTY_TYPE_FLOAT:uint = 3;
        public static const PROPERTY_TYPE_BOOL:uint = 4;
        public static const PROPERTY_TYPE_COLOR:uint = 6;
        public static const PROPERTY_TYPE_STRING:uint = 1;
        public static const PROPERTY_TYPE_INT:uint = 2;
        public static const PROPERTY_TYPE_URL:uint = 5;

        public function CSS(_arg1:String=null){
            m_id = g_idSource++;
            m_loader = new ResourceLoader();
            m_cssVariables = {};
            if (_arg1){
                m_url = _arg1;
            };
            m_loader.addEventListener(Event.COMPLETE, loader_complete);
        }
        override public function content(){
            return (m_declarationList);
        }
        override public function cancel():void{
            log("w cancel for CSS is not implemented yet!");
            m_isCancelled = true;
            dispatchEvent(new ResourceEvent(Event.CANCEL));
        }
        protected function parseImportsInCSSSegmentWithIndex(_arg1:int, _arg2:String):void{
            var _local5:CSSSegment;
            var _local8:String;
            var _local9:int;
            var _local10:CSSImport;
            var _local11:String;
            var _local12:String;
            var _local3:String = cleanupCSS(m_cssSegments[_arg1]);
            var _local4:Array = _local3.split("@import");
            var _local6:Array = [];
            _local5 = new CSSSegment();
            _local5.setContent(_local4[0]);
            _local5.setURL(_arg2);
            m_cssSegments[_arg1] = _local5;
            var _local7 = 1;
            while (_local7 < _local4.length) {
                _local11 = _local4[_local7];
                _local9 = _local11.indexOf(";");
                _local8 = CSSParsingHelper.parseURL(_local11.substring(0, _local9), _arg2);
                m_stylesheetURLs.push(_local8);
                _local10 = cssImportWithURL(_local8);
                m_importQueue.push(_local10);
                m_importsTotal++;
                _local6.push(_local10);
                _local12 = _local11.substr((_local9 + 1));
                if (!StringUtil.isWhitespace(_local12)){
                    _local5 = new CSSSegment();
                    _local5.setContent(_local12);
                    _local5.setURL(_arg2);
                    _local6.push(_local5);
                };
                _local7++;
            };
            if (_local6.length){
                m_cssSegments.splice.apply(m_cssSegments, [(_arg1 + 1), 0].concat(_local6));
            };
        }
        protected function parseCSSVariables():void{
            var _local2:CSSSegment;
            var _local3:Array;
            var _local4:int;
            var _local5:String;
            var _local6:int;
            var _local7:int;
            var _local8:int;
            var _local9:String;
            var _local10:int;
            var _local11:String;
            var _local12:String;
            var _local1:int;
            while (_local1 < m_cssSegments.length) {
                if ((m_cssSegments[_local1] is CSSImport)){
                    log(("w CSS import not loaded: " + CSSImport(m_cssSegments[_local1]).url()));
                    m_cssSegments.splice(_local1, 1);
                    _local1--;
                } else {
                    _local2 = CSSSegment(m_cssSegments[_local1]);
                    _local3 = _local2.content().split("@define ");
                    _local4 = 1;
                    while (_local4 < _local3.length) {
                        _local5 = _local3[_local4];
                        _local6 = _local5.indexOf(";\n");
                        _local7 = _local5.indexOf(";");
                        _local8 = ((_local7 == _local6)) ? (_local7 + 1) : _local7;
                        _local9 = _local5.substring(0, _local7);
                        _local10 = _local5.indexOf(" ");
                        _local11 = _local9.substring(0, _local10);
                        _local12 = _local9.substring((_local10 + 1), _local7);
                        addCSSVariableWithNameAndValue(_local11, _local12);
                        _local3[_local4] = _local5.substr((_local8 + 1));
                        _local4++;
                    };
                    _local2.setContent(_local3.join(""));
                };
                _local1++;
            };
        }
        protected function replaceCSSVariables():void{
            var _local2:CSSSegment;
            var _local3:String;
            var _local4:String;
            var _local5:String;
            var _local1:int = m_cssSegments.length;
            while (_local1--) {
                _local2 = CSSSegment(m_cssSegments[_local1]);
                _local3 = _local2.content();
                for (_local4 in m_cssVariables) {
                    _local5 = m_cssVariables[_local4];
                    _local3 = _local3.split((("${" + _local4) + "}")).join(_local5);
                };
                _local2.setContent(_local3);
            };
        }
        public function getStyleForSelectorPath(_arg1:String):CSSDeclaration{
            return (getStyleForEscapedSelectorPath(escapeSelectorPath(_arg1)));
        }
        override public function bytesTotal():int{
            return (m_importsTotal);
        }
        protected function imagePreloader_complete(_arg1:CommandEvent):void{
            notifyComplete(true);
        }
        protected function dequeueImports():void{
            var _local1:int;
            while (_local1 < m_importQueue.length) {
                m_loader.addResource(m_importQueue[_local1]);
                _local1++;
            };
            m_importQueue = [];
            m_loader.execute();
        }
        public function registerProperty(_arg1:String, _arg2:uint, _arg3:Boolean=false):void{
            RuntimeParser.registerProperty(_arg1, _arg2, _arg3);
        }
        override protected function notifyComplete(_arg1:Boolean):void{
            m_didFinishLoading = true;
            dispatchEvent(new CommandEvent(Event.COMPLETE, _arg1));
        }
        public function setBaseURL(_arg1:String):void{
            m_baseURL = _arg1;
        }
        public function getStyleForEscapedSelectorPath(_arg1:String):CSSDeclaration{
            if (!m_declarationList){
                return (new CSSDeclaration());
            };
            return (m_declarationList.getStyleForSelectorsPath(_arg1));
        }
        protected function cssImportWithURL(_arg1:String):CSSImport{
            var _local2:CSSImport = new CSSImport(this);
            _local2.setTimeout(timeout());
            _local2.setRetryTimes(retryTimes());
            _local2.setForceReload(forceReload());
            _local2.setURL(_arg1);
            return (_local2);
        }
        override public function setURL(_arg1:String):void{
            if (!m_cssSegments){
                m_cssSegments = new IndexedArray();
            };
            m_url = _arg1;
            m_cssFile = cssImportWithURL(_arg1);
            m_cssSegments[0] = m_cssFile;
        }
        protected function enqueuePreloadableProperty(_arg1:CSSProperty):void{
            if (!m_imagePreloadingResource){
                m_imagePreloadingResource = new ResourceLoader();
            };
            var _local2:BitmapResource = new BitmapResource((_arg1.valueOf() as String), true);
            _local2.setCacheBitmap(true);
            _local2.setCloneBitmap(false);
            m_imagePreloadingResource.addResource(_local2);
        }
        public function resolveImport(_arg1:CSSImport):void{
            var _local2:int = m_cssSegments.getIndex(_arg1);
            m_cssSegments[_local2] = _arg1.data();
            parseImportsInCSSSegmentWithIndex(_local2, _arg1.url());
            m_importsLoaded++;
            dispatchEvent(new ResourceEvent(ResourceEvent.PROGRESS));
        }
        override public function didSucceed():Boolean{
            return (true);
        }
        public function addCSSVariableWithNameAndValue(_arg1:String, _arg2:String):void{
            if (m_cssVariables[_arg1] != null){
                log((((((("w Warning! CSS Variable with name " + _arg1) + " is already defined! (") + m_cssVariables[_arg1]) + " -> ") + _arg2) + ")"));
                return;
            };
            m_cssVariables[_arg1] = _arg2;
        }
        override public function bytesLoaded():int{
            return (m_importsLoaded);
        }
        public function stylesheetURLs():Array{
            return (m_stylesheetURLs);
        }
        protected function parseCSSSegment(_arg1:CSSSegment):Boolean{
            var _local6:Array;
            var _local7:CSSDeclaration;
            var _local8:CSSProperty;
            var _local9:Array;
            var _local10:int;
            var _local11:int;
            var _local12:CSSProperty;
            var _local13:String;
            var _local2:int = getTimer();
            var _local3:String = _arg1.url();
            var _local4:Array = _arg1.content().split("}");
            _local4.pop();
            var _local5:int;
            while (_local5 < _local4.length) {
                _local6 = _local4[_local5].split("{");
                if (_local6.length == 2){
                    _local7 = CSSParsingHelper.parseDeclarationString(_local6[1], _local3);
                    _local8 = _local7.getStyle("backgroundImagePreload");
                    if (((_local8) && (_local8.valueOf()))){
                        _local12 = _local7.getStyle("backgroundImage");
                        ((_local12) && (enqueuePreloadableProperty(_local12)));
                    };
                    _local9 = _local6[0].split("\n").join(" ").split("  ").join(" ").split(", ").join(",").split(",");
                    _local10 = _local9.length;
                    _local11 = 0;
                    while (_local11 < _local10) {
                        _local13 = _local9[_local11];
                        m_declarationList.addDeclaration(_local7, _local13);
                        _local11++;
                    };
                };
                _local5++;
            };
            m_parseTime = (m_parseTime + (getTimer() - _local2));
            return (true);
        }
        public function baseURL():String{
            return (m_baseURL);
        }
        protected function cleanupCSS(_arg1:String):String{
            var _local5:String;
            var _local6:int;
            var _local2:int = getTimer();
            var _local3:Array = _arg1.split("/*");
            var _local4:uint = _local3.length;
            while (_local4-- > 1) {
                _local5 = _local3[_local4];
                _local6 = _local5.indexOf("*/");
                if (_local6 == -1){
                    _local3[_local4] = "";
                } else {
                    _local3[_local4] = _local5.substr((_local6 + 2));
                };
            };
            _arg1 = _local3.join("");
            _arg1 = _arg1.split("\r\n").join("\n");
            _arg1 = _arg1.split("\r").join("\n");
            while (_arg1.indexOf("\n\n") > -1) {
                _arg1 = _arg1.split("\n\n").join("\n");
            };
            _arg1 = _arg1.split("\t").join(" ");
            while (_arg1.indexOf("  ") > -1) {
                _arg1 = _arg1.split("  ").join(" ");
            };
            _arg1 = _arg1.split("\n ").join("\n").split(" \n").join("\n").split(" {").join("{").split("\n{").join("{").split("}\n").join("}").split("( ").join("(").split(" )").join(")").split("'").join("").split("\"").join("").split(" !important").join("!important").split("> ").join(">").split(" >").join(">").split(">").join("> ").split(" ,").join(",").split(", ").join(",").split(" ;").join(";").split("{\n}").join("{}");
            _arg1 = StringUtil.lTrim(_arg1);
            m_cleanupTime = (m_cleanupTime + (getTimer() - _local2));
            return (_arg1);
        }
        public function addCSSVariablesFromObject(_arg1:Object):void{
            var _local2:String;
            for (_local2 in _arg1) {
                addCSSVariableWithNameAndValue(_local2, _arg1[_local2]);
            };
        }
        override public function isExecuting():Boolean{
            return (false);
        }
        protected function loader_complete(_arg1:CommandEvent):void{
            if (!_arg1.success){
                log((("e CSS \"" + url()) + "\" could not be loaded"));
                return;
            };
            if (m_importQueue.length){
                dequeueImports();
                return;
            };
            parseCSS();
        }
        protected function parseCSS():void{
            var _local1:Boolean;
            var _local4:CSSSegment;
            parseCSSVariables();
            replaceCSSVariables();
            var _local2:int;
            while (_local2 < m_cssSegments.length) {
                _local4 = m_cssSegments[_local2];
                _local1 = parseCSSSegment(_local4);
                if (!_local1){
                    log(((("f Error parsing css file \"" + _local4.url()) + "\". Make sure that all ") + "your used variables are defined (if any)."));
                    break;
                };
                _local2++;
            };
            m_cssSegments = new IndexedArray();
            var _local3 = ((((((("----------------- CSS Stats -----------------\n" + " time spent cleaning up strings: ") + m_cleanupTime) + " ms\n") + " time spent parsing: ") + m_parseTime) + " ms\n") + "---------------------------------------------");
            log(("d " + _local3));
            if (!m_imagePreloadingResource){
                dispatchEvent(new CommandEvent(Event.COMPLETE, _local1));
            } else {
                m_imagePreloadingResource.addEventListener(Event.COMPLETE, imagePreloader_complete);
                m_imagePreloadingResource.execute();
            };
        }
        override public function execute(... _args):void{
            if (m_url == null){
                throw (new Error(("You didn't specify an URL for your " + "CSS resource! Make sure you do this before calling execute!")));
            };
            if (!m_runtimeParserRegistered){
                CSSDeclaration.registerPropertyCollection(RuntimeParser);
                m_runtimeParserRegistered = true;
            };
            m_didFinishLoading = false;
            m_isCancelled = false;
            m_parseTime = 0;
            m_cleanupTime = 0;
            m_importsLoaded = 0;
            m_importsTotal = 1;
            m_declarationList = new CSSDeclarationList();
            m_stylesheetURLs = [];
            m_importQueue = [];
            m_cssSegments = new IndexedArray();
            setURL(m_url);
            m_cssFile.setURL(CSSParsingHelper.resolvePathAgainstPath(url(), baseURL()));
            m_stylesheetURLs.push(m_cssFile.url());
            m_loader.addResource(m_cssFile);
            m_loader.execute();
        }
        public function parseCSSStringWithBaseURL(_arg1:String, _arg2:String):void{
            _arg2 = ((_arg2) || ("/"));
            var _local3 = (m_importsLoaded < m_importsTotal);
            m_cssSegments.push(_arg1);
            parseImportsInCSSSegmentWithIndex((m_cssSegments.length - 1), _arg2);
            if (_local3){
                return;
            };
            if (m_importQueue.length){
                dequeueImports();
                return;
            };
            var _local4:Boolean = parseCSSSegment(m_cssSegments[0]);
            if (!_local4){
                log("e Error! Couldn't parse css string!");
            };
        }

        public static function escapeSelectorPath(_arg1:String):String{
            var _local2:Array = _arg1.split("@");
            var _local3:String = String(_local2.pop());
            _local3 = _local3.split(" ").join("@ @").split("#").join("@@#").split(".").join("@@.").split(":").join("@@:");
            if (_local3.charAt(0) != "@"){
                _local3 = ("@" + _local3);
            } else {
                if (_local3.charAt(1) == "@"){
                    _local3 = _local3.substr(1);
                };
            };
            if (_local3.charAt((_local3.length - 1)) != "@"){
                _local3 = (_local3 + "@");
            };
            _local2.push(_local3);
            return (_local2.join("@"));
        }

    }
}//package reprise.css 
